﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BestBuy.DAL;
using BestBuy.Common;
using BestBuy.DTO;
namespace BestBuy.PageUser
{
    public partial class Message : System.Web.UI.Page
    {        
        private static List<DTOMessage> limess;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["IDDN"].ToString().Equals(""))
            {
                Response.Redirect("~/Account/Login.aspx");
            }
            if (!IsPostBack)
            {
                loadUnread();
                loadListMessage();
            }
        }
        private void loadUnread()
        {
            unread.Text = CMessage.countMessage(int.Parse(Session["IDDN"].ToString())).ToString();
            lbrecord.Text = CMessage.countTotalMessage(int.Parse(Session["IDDN"].ToString())).ToString();
        }
        private void loadListMessage()
        {
            limess = new List<DTOMessage>();            
            List<BMessage> mess = CMessage.getListMessage(int.Parse(Session["IDDN"].ToString()));
            foreach (var item in mess)
            {
                DTOMessage dto = new DTOMessage();
                dto.IdMessage = item.ID;
                dto.IdSender = item.IDTKSender;
                if (item.Subject.Length < 30)
                {
                    dto.Title = item.Subject;
                }
                else
                {
                    dto.Title = item.Subject.Substring(0, 30);
                }
                dto.Subject = item.Subject;
                dto.NameSender = CMessage.getNameSender(item.IDTKSender);
                dto.Date = item.Date;
                dto.Source = item.Source;
                int id = item.Source;
                if (id == 1)
                {
                    dto.NameSource = "Product";
                }
                if (id == 2)
                {
                    dto.NameSource = "BuyOffer";
                }
                if (id == 3)
                {
                    dto.NameSource = "SellOffer";
                }
                if (id == 4)
                {
                    dto.NameSource = "Company";
                }
                if (id == 5)
                {
                    dto.NameSource = CMessage.getAccount(item.IDTKSender);
                }
                dto.Status = item.Status;
                dto.IdContact = item.IDContact;
                dto.Country = CMessage.getNameCountry(item.IDTKSender);
                limess.Add(dto);
            }
            gvListProduct.DataSource = limess;
            gvListProduct.DataBind();
        }
        protected void gvListProduct_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //kiem tra unverified
                int status = (int)DataBinder.Eval(e.Row.DataItem, "Status");
                int IdMessage = (int)DataBinder.Eval(e.Row.DataItem, "IdMessage");
                switch (status)
                {
                    case (int)MessageStatus.SentUnread:
                        HyperLink unsubject = e.Row.FindControl("HpUSubject") as HyperLink;
                        Label unsender = e.Row.FindControl("lbUSender") as Label;
                        Label unsource = e.Row.FindControl("lbUSource") as Label;
                        if (unsender != null && unsubject !=null & unsource!=null)
                        {
                            unsender.Visible = true;
                            unsubject.Visible = true;
                            unsource.Visible = true;
                            unsubject.NavigateUrl = "ReadMessage.aspx?IdMessage=" + IdMessage;                           
                        }
                        break;
                    case (int)MessageStatus.Read:
                        HyperLink Rsubject = e.Row.FindControl("HpRSubject") as HyperLink;
                        Label Rsender = e.Row.FindControl("lbRSender") as Label;
                        Label Rsource = e.Row.FindControl("lbRSource") as Label;
                        if (Rsender != null && Rsubject != null & Rsource != null)
                        {
                            Rsender.Visible = true;
                            Rsubject.Visible = true;
                            Rsource.Visible = true;
                            Rsubject.NavigateUrl = "ReadMessage.aspx?IdMessage=" + IdMessage;                            
                        }
                        break;
                }
            }
        }

        protected void linkDell_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < gvListProduct.Rows.Count; i++)
            {
                CheckBox cb = (CheckBox)gvListProduct.Rows[i].FindControl("chkXoa");
                if (cb.Checked)
                {
                    foreach (var ite in limess)
                    {
                        if (ite.IdMessage == Convert.ToInt32(((Label)gvListProduct.Rows[i].FindControl("lbIdSanPham")).Text))
                        {
                            int status = (int)MessageStatus.Erase;
                            CMessage.eraseMessage(ite.IdMessage, status);
                        }
                    }
                }
            }
            loadListMessage();
        }

        protected void linkReply_Click(object sender, EventArgs e)
        {
            DTOListReply.List = new List<DTOReplys>();
            for (int i = 0; i < gvListProduct.Rows.Count; i++)
            {
                CheckBox cb = (CheckBox)gvListProduct.Rows[i].FindControl("chkXoa");
                if (cb.Checked)
                {
                    DTOReplys rep = new DTOReplys();
                    rep.IdSender = Convert.ToInt32(((Label)gvListProduct.Rows[i].FindControl("lbIdSender")).Text);
                    rep.NameSender = ((Label)gvListProduct.Rows[i].FindControl("lbRSender")).Text;
                    rep.Idmess = Convert.ToInt32(((Label)gvListProduct.Rows[i].FindControl("lbIdSanPham")).Text);
                    DTOListReply.List.Add(rep);                    
                }
            }
            Response.Redirect("ReplysMessage.aspx");
        }
    }
}